﻿
using Xacml.Core.Interfaces;

namespace Xacml.Core.Runtime
{
  public class PolicyCombiningAlgorithmOnlyOneApplicable : IPolicyCombiningAlgorithm
  {
    public Decision Evaluate(EvaluationContext context, IMatchEvaluableCollection policies)
    {
      bool flag = false;
      Policy policy = (Policy) null;
      for (int index = 0; index < policies.Count; ++index)
      {
        switch (((Policy) policies[index]).Match(context))
        {
          case TargetEvaluationValue.Indeterminate:
            context.ProcessingError = true;
            context.TraceContextValues();
            return Decision.Indeterminate;
          case TargetEvaluationValue.Match:
            if (flag)
            {
              context.ProcessingError = true;
              context.TraceContextValues();
              return Decision.Indeterminate;
            }
            flag = true;
            policy = (Policy) policies[index];
            break;
        }
      }
      if (!flag)
        return Decision.NotApplicable;
      Decision decision = policy.Evaluate(context);
      context.TraceContextValues();
      if (decision == Decision.Deny || decision == Decision.Permit)
      {
        context.ProcessingError = false;
        context.IsMissingAttribute = false;
      }
      return decision;
    }
  }
}
